package cn.iocoder.yudao.module.train.controller.admin.scanrecord;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.ScanRecordDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.scanrecord.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.scanrecord.ScanRecordDO;
import cn.iocoder.yudao.module.train.service.scanrecord.ScanRecordService;

@Tag(name = "管理后台 - 课程签到记录")
@RestController
@RequestMapping("/train/scan-record")
@Validated
public class ScanRecordController {

    @Resource
    private ScanRecordService scanRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建课程签到记录")
    @PreAuthorize("@ss.hasPermission('train:scan-record:create')")
    public CommonResult<Integer> createScanRecord(@Valid @RequestBody ScanRecordSaveReqVO createReqVO) {
        return success(scanRecordService.createScanRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程签到记录")
    @PreAuthorize("@ss.hasPermission('train:scan-record:update')")
    public CommonResult<Boolean> updateScanRecord(@Valid @RequestBody ScanRecordSaveReqVO updateReqVO) {
        scanRecordService.updateScanRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程签到记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:scan-record:delete')")
    public CommonResult<Boolean> deleteScanRecord(@RequestParam("id") Integer id) {
        scanRecordService.deleteScanRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课程签到记录")
                @PreAuthorize("@ss.hasPermission('train:scan-record:delete')")
    public CommonResult<Boolean> deleteScanRecordList(@RequestParam("ids") List<Integer> ids) {
        scanRecordService.deleteScanRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程签到记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:scan-record:query')")
    public CommonResult<ScanRecordRespVO> getScanRecord(@RequestParam("id") Integer id) {
        ScanRecordDO scanRecord = scanRecordService.getScanRecord(id);
        return success(BeanUtils.toBean(scanRecord, ScanRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程签到记录分页")
    @PreAuthorize("@ss.hasPermission('train:scan-record:query')")
    public CommonResult<PageResult<ScanRecordRespVO>> getScanRecordPage(@Valid ScanRecordPageReqVO pageReqVO) {
        PageResult<ScanRecordDTO> pageResult = scanRecordService.getScanRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ScanRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程签到记录 Excel")
    @PreAuthorize("@ss.hasPermission('train:scan-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScanRecordExcel(@Valid ScanRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScanRecordDTO> list = scanRecordService.getScanRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程签到记录.xls", "数据", ScanRecordRespVO.class,
                        BeanUtils.toBean(list, ScanRecordRespVO.class));
    }

}
